package com.example.bookmall.controller.adminSystem;

import com.example.bookmall.entity.Result;
import com.example.bookmall.entity.usercore.DeleteUserRequest;
import com.example.bookmall.entity.usercore.User;
import com.example.bookmall.entity.usercore.UserDto;
import com.example.bookmall.entity.usercore.UserSearchRequest;
import com.example.bookmall.service.AdminUserService;
import com.example.bookmall.service.UserService;
import com.example.bookmall.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class adminController {


    @Autowired
    private AdminUserService adminService;
    @Autowired
    private UserService userService;

    //管理员查询用户
    @GetMapping("/list")
    public Result listUser(){
        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User user = userService.findByUserName(userName);

        if (user.getRole() == 0||  user.getRole() == 1) {
            return Result.error("权限不足");
        }
        List<User> userList=new ArrayList<>();

        if(user.getRole()==2){
            //普通管理员 返回所有用户数据 这里需要过滤一下
           userList= userService.listCommonUser();
        }
        if(user.getRole()==3){
            //超级管理员 返回普通用户数据和管理员数据
            userList=userService.listAllUser();
        }

        return Result.success(userList);
    }

    //删除用户数据
    //超级管理员才有资格删除 不能删除角色为管理员的用户
    @DeleteMapping("/delete")
    public Result deleteUser(@RequestBody DeleteUserRequest deleteUserRequest) {
        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User user = userService.findByUserName(userName);

        if (user.getRole() == 1) {
            return Result.error("权限不足");
        }

       return adminService.deleteUser(deleteUserRequest.getUserId());

    }

    //管理员 根据电话号码查询用户
    @GetMapping("/findByPhone")
    public Result findByPhone(String phone){
        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User user = userService.findByUserName(userName);
        if (user.getRole() == 1) {
            return Result.error("权限不足");
        }
        return adminService.findByPhone(phone);

    }

    //管理员 根据用户名查询用户
    @GetMapping("/findByName")
    public Result findByName(String username){
        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User user = userService.findByUserName(userName);
        if (user.getRole() == 1) {
            return Result.error("权限不足");
        }
        return adminService.findByUserName(username);
    }


    //管理员 根据邮箱查询用户
    @GetMapping("/findByEmail")
    public Result findByEmail(String email){
        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User user = userService.findByUserName(userName);
        if (user.getRole() == 1) {
            return Result.error("权限不足");
        }
        return adminService.findByEmail(email);
    }


    //可以修改普通用户数据 姓名  邮箱 手机号码 状态
    @PostMapping("/modify")
    public Result modifyUser(@RequestBody User user){

        //当前用户
        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User currentuser = userService.findByUserName(userName);

        if (currentuser.getRole() == 0||  currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }
        return adminService.modifyUser(user.getUserId(),user.getUsername(),user.getEmail(),user.getPhone(),user.getStatus(),user.getGender());

    }


    //添加用户
    //信息->用户昵称 手机号码 用户名称 性别 邮箱 密码(默认123456) 状态(默认正常 0) 角色(普通用户user)
    @PostMapping("/add")
    public Result addUser(@RequestBody UserDto userDto){

        Map<String, Object> map = ThreadLocalUtil.get();
        String userName = (String) map.get("username");
        User currentuser = userService.findByUserName(userName);

        if (currentuser.getRole() == 0||  currentuser.getRole() == 1) {
            return Result.error("权限不足");
        }

        System.out.println("添加用户"+userDto);

        return adminService.addUser(userDto);

    }


}
